using System;
namespace Thunder
{
	public static class DateTimeExtensions
	{
		private static DateTime unixTimestampBegin = new DateTime(1970, 1, 1, 0, 0, 0);		
		
		public static string ToFullString(this DateTime dt)
        {
            return dt.ToString("dd.MM.yyyy HH:mm:ss.fffffff");
        }
		
        public static string ToFullTimeString(this DateTime dt)
        {
            return dt.ToString("HH:mm:ss.fffffff");
        }
		
        public static string ToANSISQLString(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }
		
        public static string ToISO8601String(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-ddTHH:mm:ss");
        }
		
		public static DateTime AddWeeks(this DateTime dt, int weeks)
		{
			return dt.AddDays(weeks * 7);	
		}
		
		public static DateTime PrevDayOfWeek(this DateTime dt, DayOfWeek day)
		{
			if (dt.DayOfWeek == day)
				return dt.AddWeeks(-1);
			return dt.AddDays((day - dt.DayOfWeek) > 0 ? (dt.DayOfWeek - day) : (day - dt.DayOfWeek));
		}
		
		public static DateTime NextDayOfWeek(this DateTime dt, DayOfWeek day)
		{
			if (dt.DayOfWeek == day)
				return dt.AddWeeks(1);
			return dt.AddDays((dt.DayOfWeek - day) > 0 ? (dt.DayOfWeek - day) : (day - dt.DayOfWeek));
		}
		
		public static int GetDaysInMonth(this DateTime dt)
		{
			return DateTime.DaysInMonth(dt.Year, dt.Month);
		}
		
		public static DateTime GetFirstDayInMonth(this DateTime dt)
		{
			return new DateTime(dt.Year, dt.Month, 1, dt.Hour, dt.Minute, dt.Second, dt.Millisecond);	
		}
		
		public static DateTime GetLastDayInMonth(this DateTime dt)
		{
			return new DateTime(dt.Year, dt.Month, dt.GetDaysInMonth(), dt.Hour, dt.Minute, dt.Second, dt.Millisecond);	
		}	
		
		public static UInt32 ToUnixTimestamp(this DateTime dt)
		{
			return dt.ToUnixTimestamp(TimeZone.CurrentTimeZone);	
		}
		
		public static UInt32 ToUnixTimestamp(this DateTime dt, TimeZone tz)
		{
			return (UInt32)(dt.ToUTCUnixTimestamp() - tz.GetUtcOffset(dt).TotalSeconds);
		}
		
		public static UInt32 ToUnixTimestamp(this DateTime dt, TimeZoneInfo tzi)
		{
			return (UInt32)(dt.ToUTCUnixTimestamp() - tzi.GetUtcOffset(dt).TotalSeconds);
		}
		
		public static UInt32 ToUTCUnixTimestamp(this DateTime dt)
		{
			return (UInt32)dt.Subtract(unixTimestampBegin).TotalSeconds;
		}
	}
}

